<template>
    <el-card class="forget" v-if="handleType == 'forget'">
        <div class="header">找回密码</div>
        <el-form :model="form" @submit.prevent="handleLogin">

            <el-form-item>
                <el-icon size="25">
                    <User />
                </el-icon>
                <el-input v-model="form.username" placeholder="请输入注册用户名" required></el-input>
            </el-form-item>

            <el-form-item>
                <el-icon size="25">
                    <Lock />
                </el-icon>
                <el-input show-password type="password" v-model="form.password" placeholder="请输入新密码"
                    required></el-input>
            </el-form-item>

            <el-form-item>
                <el-icon size="25">
                    <Lock />
                </el-icon>
                <el-input show-password type="password" v-model="form.verifyPassword" placeholder="请确定新密码"
                    required></el-input>
            </el-form-item>
        </el-form>
        <el-button type="primary" @click="handleLogin">找回密码</el-button>
        <div class="register">已有账号，<span style="color: blue;cursor: pointer;"
                @click="handleChangeType('login')">直接登录</span>
        </div>
    </el-card>
</template>
<script setup lang="ts">
import { ref, defineProps, defineEmits } from "vue"
import { updateData } from "../../../api/api"
const props = defineProps({
    handleType: String
})
const emit = defineEmits()

const handleChangeType = (type: string) => {
    emit("handleChangeType", type)
}

let form = ref({
    username: '',
    password: '',
    verifyPassword: ''
});


const handleLogin = async () => {
    if (form.value.password !== form.value.verifyPassword) {
        console.log("两次密码不一致");
        return;
    }
    const res = await updateData("/accounts", {
        username: form.value.username,
        password: form.value.password
    })
    console.log("res", res);
    if (res.code != 200) {
        return;
    }
    emit("handleChangeType", "login")

}
</script>
<style scoped lang="scss">
.el-card {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    box-sizing: border-box;
    padding: 20px 0;

    :deep() .el-card__body {
        padding: 0 !important;
        width: 80%;
    }

    .header {
        width: 100%;
        text-align: center;
        font-size: 20px;
        padding: 20px 0;
        letter-spacing: 3px;
    }

    .el-form {
        margin-top: 20px;

        .el-form-item {
            width: 100%;
            display: flex;
            align-items: center;
            justify-content: center;


            .el-input {
                width: 85%;
                margin-left: 10px;
            }

            &:not(:last-child) {
                margin-bottom: 20px;
            }
        }
    }

    .password {
        width: 100%;
        margin-bottom: 20px;
        display: flex;
        align-items: center;
        justify-content: space-between;
        margin-top: -10px;

        .el-checkbox {
            width: 100px;
        }
    }

    .el-button {
        width: 100%;
        height: 40px;
        letter-spacing: 3px;
        font-size: 18px;
    }

    .register {
        width: 100%;
        margin-top: 20px;
        text-align: center;
    }
}
</style>